﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using Settings;
using System.Data;
using System.Data.SqlClient;

namespace SMSROUTER.Business
{
    public partial class SmsOutStatistic
    {
        public static List<SmsOutStatistic> Paging(int currentPage, int pageSize, ref int totalRecord, ref int totalMessage,
                                           string shortcode, string subs,
                                           DateTime? fromTime, DateTime? toTime,
                                           string status)
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                var resutl = context.SmsOutStatistics.AsQueryable();
                if (!string.IsNullOrWhiteSpace(shortcode))
                {
                    resutl = resutl.Where(p => p.ShortCode.Equals(shortcode));
                }
                if (!string.IsNullOrWhiteSpace(subs))
                {
                    resutl = resutl.Where(p => p.DestAddr.Equals(subs.ToNormalPhoneNumber()) || p.DestAddr.Equals(subs));
                }
                if (!string.IsNullOrWhiteSpace(status))
                {
                    resutl = resutl.Where(p => p.Status.Equals(status));
                }
                if (fromTime != null)
                {
                    resutl = resutl.Where(p => p.CreatedTime >= fromTime);
                }
                if (toTime != null)
                {
                    resutl = resutl.Where(p => p.CreatedTime <= toTime.Value.AddDays(1));
                }
                totalRecord = resutl.Count();
                totalMessage = resutl.Select(p => p.Message).ToList().Sum(p => p.GetMessageCount());
                return resutl
                    .OrderByDescending(p=>p.SentTime)
                    .Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize)
                    .Take(pageSize)
                    .ToList();
            }
        }
        
        public static List<SmsOutStatistic> GetByMoGuid(Guid smsInGuid)
        {
            string query = "SELECT * FROM SmsOutStatistic sms WHERE sms.SmsInGUID = @smsInGuid";
            return ObjectClass.CreateList<SmsOutStatistic>(SqlHelper.ExecuteReader(AppSetting.ConnectionString, CommandType.Text, query, new SqlParameter("@smsInGuid", smsInGuid)));
        }

        public static void Retry(DataTable dtSource)
        {
            if (dtSource == null || dtSource.Rows.Count == 0) return;
            try
            {
                SqlParameter param = new SqlParameter("@Ids", dtSource);
                param.SqlDbType = SqlDbType.Structured;
                param.TypeName = "dbo.BigIntTableType";
                DataAccess.Execute("usp_SmsOutStatistic_RetryFailData", param, new SqlParameter("@RetryOnFail", AppSetting.RetryOnFailReqTimes), new SqlParameter("@RetryOnTimeout", AppSetting.RetryOnTimeoutReqTimes));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
